package com.cn.mmd.controller;


import com.cn.mmd.model.ShiroUserRole;
import com.cn.mmd.model.StudentApp;
import com.cn.mmd.model.TeacherData;
import com.cn.mmd.service.ShiroUserRoleService;
import com.cn.mmd.service.StudentAppService;
import com.cn.mmd.service.TeacherDataLService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * Created by Administrator on 2016/9/13 0013.
 */
@Controller
public class TokenController {

    @Autowired
    private TeacherDataLService teacherDataLService;

    @Autowired
    private StudentAppService studentAppService;

    @Autowired
    private ShiroUserRoleService shiroUserRoleService;

    public synchronized Object token(String id,String token){
        TeacherData teacherData;
        teacherData =teacherDataLService.selectByPrimaryKey(id);
        if (teacherData!=null&& DigestUtils.md5Hex(id+teacherData.getTeacherpassword()).equals(token)) {
            return teacherData;
        }else {
            return null;
        }
    }

    public synchronized Object token1(String id,String token){
        StudentApp studentApp;
        studentApp = studentAppService.selectByPrimaryKey(id);
        if (studentApp!=null&&DigestUtils.md5Hex(id+studentApp.getStudentpassword()).equals(token)){
            return studentApp;
        }else {
            return null;
        }
    }

    public synchronized Object tokenLead(String id,int rId,String token){
        TeacherData teacherData;
        teacherData =teacherDataLService.selectByPrimaryKey(id);
        ShiroUserRole shiroUserRole = new ShiroUserRole();
        shiroUserRole.setUserId(id);
        shiroUserRole.setRoleId(rId);
        shiroUserRole = shiroUserRoleService.selectByAll(shiroUserRole);
        if (teacherData!=null&&shiroUserRole!=null&&DigestUtils.md5Hex(id+teacherData.getTeacherpassword()+rId).equals(token)){
            return teacherData;
        }else {
            return null;
        }
    }
}
